package com.good.jzhouse.config;

import com.good.jzhouse.bean.Users;
import com.good.jzhouse.dao.UsersDao;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @Author: xiaolei
 * @Date: 2021/3/13 10:52
 */
public class RayRealm extends AuthorizingRealm {

    @Resource
    UsersDao dao;
    @Override   //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override   //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String principal = (String) token.getPrincipal();    //获取用户名
        //Application工具类拿组件
        Users user =dao.selectByPrincipal(principal);
        if (ObjectUtils.isEmpty(user)){
            return null;
        }else {
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                    principal,
                    user.getPassword(),
                    ByteSource.Util.bytes(user.getPhone()),
                    this.getName());
            return  simpleAuthenticationInfo;
        }
    }
}
